home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 20.0 KB | 652 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWFxMath.h
- // Release Version: $ 1.0d11 $
- //
- // Copyright: © 1993, 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWFXMATH_H
- #define FWFXMATH_H
-
- // ----- Foundation Includes -----
-
- #ifndef FWSTDDEF_H
- #include "FWStdDef.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _ODMath_
- #include <ODMath.h>
- #endif
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- //========================================================================================
- // Definitions and utilities
-
- // These have to be done as macros because Metrowerks C++ 1.2.2 has difficulties
- // with inlines that call other inlines
-
- #define FW_PrivIntToFixed(i) (ODFixed)((long) (i) << 16)
- #define FW_PrivDoubleToFixed(d) (ODFixed)((double) (d) * 65536.0)
-
- #define FW_PrivFixedToInt(f) (int)((ODFixed)(f + 0x00008000) >> 16)
- #define FW_PrivFixedToTruncatedInt(f) (int)((ODFixed)(f + 0x00000010) >> 16)
-
- #define FW_PrivFixedToDouble(f) (double)((ODFixed)(f) / 65536.0)
-
- //========================================================================================
- // Forward class declaration
- //========================================================================================
-
- class FW_CLASS_ATTR FW_CWide;
- class FW_CLASS_ATTR FW_CFixed;
- class FW_CLASS_ATTR FW_CWritableStream;
- class FW_CLASS_ATTR FW_CReadableStream;
-
- //========================================================================================
- // Global operators << and >>
- //========================================================================================
-
- FW_FUNC_ATTR const FW_CWritableStream& operator<<(const FW_CWritableStream& stream, const FW_CFixed& fx);
- FW_FUNC_ATTR const FW_CReadableStream& operator>>(const FW_CReadableStream& stream, FW_CFixed& fx);
-
- //========================================================================================
- // CLASS FW_CFixed
- //========================================================================================
-
- class FW_CLASS_ATTR FW_CFixed
- {
- public:
- // ----- Construction/destruction -----
-
- friend inline FW_CFixed FW_IntToFixed(int i);
- friend inline FW_CFixed FW_DoubleToFixed(double d);
- friend inline FW_CFixed FW_ODFixedToFixed(ODFixed f);
-
- FW_CFixed(); // No defined value
-
- // ----- Copying -----
-
- // Using the compiler generate ones makes the optimizer work better
- // inline CFixed(const CFixed& f) : fRep(f.fRep) { }
- // inline CFixed& operator=(const CFixed& f) { fRep = f.fRep; return *this; }
-
- // ----- Input/output
-
- friend FW_FUNC_ATTR const FW_CWritableStream& operator<<(const FW_CWritableStream& stream, const FW_CFixed& fx);
- friend FW_FUNC_ATTR const FW_CReadableStream& operator>>(const FW_CReadableStream& stream, FW_CFixed& fx);
-
- // ----- Assignment operators -----
-
- FW_CFixed& SetInt(int i);
- FW_CFixed& SetDouble(double d);
- FW_CFixed& SetODFixed(ODFixed f);
-
- // ----- Converion operators -----
-
- int AsInt() const;
- int AsTruncatedInt() const;
- double AsDouble() const;
- ODFixed AsODFixed() const;
-
- operator void* () const { return (void*) fRep; }
- // This is only for convenience of testing against 0
-
- // ----- Arithmetic operators -----
-
- FW_CFixed operator - () const;
- FW_CFixed operator + () const;
-
- FW_CFixed& operator += (FW_CFixed fixed);
- FW_CFixed& operator -= (FW_CFixed fixed);
- FW_CFixed& operator *= (FW_CFixed fixed);
- FW_CFixed& operator /= (FW_CFixed fixed);
-
- FW_CFixed Half() const;
- FW_CFixed MultipliedByInt(int i) const;
- FW_CFixed DividedByInt(int i) const;
-
- FW_CFixed RoundedToInt() const;
-
- friend FW_CWide FW_FUNC_ATTR FW_WideMultiply (FW_CFixed f1, FW_CFixed f2);
- friend FW_CFixed FW_FUNC_ATTR operator / (const FW_CWide& w1, FW_CFixed f2);
-
- FW_CFixed& operator ++ (); // prefix
- void operator ++ (int i); // postfix
- FW_CFixed& operator -- (); // prefix
- void operator -- (int i); // postfix
-
- #ifdef FW_DEBUG
- friend FW_FUNC_ATTR FW_CFixed operator + (FW_CFixed f1, FW_CFixed f2);
- friend FW_FUNC_ATTR FW_CFixed operator - (FW_CFixed f1, FW_CFixed f2);
- friend FW_FUNC_ATTR FW_CFixed operator * (FW_CFixed f1, FW_CFixed f2);
- friend FW_FUNC_ATTR FW_CFixed operator / (FW_CFixed f1, FW_CFixed f2);
- #else
- friend inline FW_CFixed operator + (FW_CFixed f1, FW_CFixed f2);
- friend inline FW_CFixed operator - (FW_CFixed f1, FW_CFixed f2);
- friend inline FW_CFixed operator * (FW_CFixed f1, FW_CFixed f2);
- friend inline FW_CFixed operator / (FW_CFixed f1, FW_CFixed f2);
- #endif
-
- // ----- Equality operators -----
-
- friend inline FW_Boolean operator == (FW_CFixed f1, FW_CFixed f2);
- friend inline FW_Boolean operator != (FW_CFixed f1, FW_CFixed f2);
- friend inline FW_Boolean operator > (FW_CFixed f1, FW_CFixed f2);
- friend inline FW_Boolean operator < (FW_CFixed f1, FW_CFixed f2);
- friend inline FW_Boolean operator >= (FW_CFixed f1, FW_CFixed f2);
- friend inline FW_Boolean operator <= (FW_CFixed f1, FW_CFixed f2);
-
- // ----- Transcendental methods
-
- FW_CFixed Sin() const;
- FW_CFixed Cos() const;
- FW_CFixed Sqrt() const;
-
- // ----- Implementation
- private:
- ODFixed fRep;
-
- FW_CFixed(ODFixed rep);
- };
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::FW_CFixed
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed::FW_CFixed()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::FW_CFixed
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed::FW_CFixed(ODFixed rep) : fRep(rep)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_IntToFixed
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed FW_IntToFixed(int i)
- {
- return FW_CFixed(FW_PrivIntToFixed(i));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_DoubleToFixed
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed FW_DoubleToFixed(double d)
- {
- return FW_CFixed(FW_PrivDoubleToFixed(d));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_ODFixedToFixed
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed FW_ODFixedToFixed(ODFixed f)
- {
- return FW_CFixed(f);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::AsInt
- //----------------------------------------------------------------------------------------
-
- inline int FW_CFixed::AsInt() const
- {
- return FW_PrivFixedToInt(fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::AsTruncatedInt
- //----------------------------------------------------------------------------------------
-
- inline int FW_CFixed::AsTruncatedInt() const
- {
- return FW_PrivFixedToTruncatedInt(fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::AsDouble
- //----------------------------------------------------------------------------------------
-
- inline double FW_CFixed::AsDouble() const
- {
- return FW_PrivFixedToDouble(fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::AsODFixed
- //----------------------------------------------------------------------------------------
-
- inline ODFixed FW_CFixed::AsODFixed() const
- {
- return fRep;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::SetInt
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed& FW_CFixed::SetInt(int i)
- {
- fRep = FW_PrivIntToFixed(i);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::SetDouble
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed& FW_CFixed::SetDouble(double d)
- {
- fRep = FW_PrivDoubleToFixed(d);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::SetODFixed
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed& FW_CFixed::SetODFixed(ODFixed f)
- {
- fRep = f;
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::operator-
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed FW_CFixed::operator-() const
- {
- return FW_CFixed(-fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::operator+
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed FW_CFixed::operator+() const
- {
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::operator+=
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed& FW_CFixed::operator+=(FW_CFixed fixed)
- {
- fRep += fixed.fRep;
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::operator-=
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed& FW_CFixed::operator-=(FW_CFixed fixed)
- {
- fRep -= fixed.fRep;
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::operator*=
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed& FW_CFixed::operator*=(FW_CFixed fixed)
- {
- fRep = ODFixedMultiply(fRep, fixed.fRep);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::operator/=
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed& FW_CFixed::operator/=(FW_CFixed fixed)
- {
- fRep = ODFixedDivide(fRep, fixed.fRep);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::Half
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed FW_CFixed::Half() const
- {
- return FW_CFixed(fRep / 2);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::MultipliedByInt
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed FW_CFixed::MultipliedByInt(int i) const
- {
- return FW_CFixed(fRep * i);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::DividedByInt
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed FW_CFixed::DividedByInt(int i) const
- {
- return FW_CFixed(fRep / i);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::RoundedToInt
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed FW_CFixed::RoundedToInt() const
- {
- return FW_CFixed((fRep + 0x00008000) & 0xFFFF0000ul);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::operator++
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed& FW_CFixed::operator++() // prefix
- {
- fRep += FW_PrivIntToFixed(1);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::operator++
- //----------------------------------------------------------------------------------------
-
- inline void FW_CFixed::operator++(int /* i */) // postfix
- {
- fRep += FW_PrivIntToFixed(1);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::operator--
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed& FW_CFixed::operator--() // prefix
- {
- fRep -= FW_PrivIntToFixed(1);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed::operator--
- //----------------------------------------------------------------------------------------
-
- inline void FW_CFixed::operator--(int /* i */) // postfix
- {
- fRep -= FW_PrivIntToFixed(1);
- }
-
- //========================================================================================
- // Global functions
- //========================================================================================
-
- #ifndef FW_DEBUG
-
- inline FW_CFixed operator+(FW_CFixed f1, FW_CFixed f2)
- {
- return FW_CFixed(f1.fRep + f2.fRep);
- }
-
- inline FW_CFixed operator-(FW_CFixed f1, FW_CFixed f2)
- {
- return FW_CFixed(f1.fRep - f2.fRep);
- }
-
- inline FW_CFixed operator*(FW_CFixed f1, FW_CFixed f2)
- {
- return FW_CFixed(ODFixedMultiply(f1.fRep, f2.fRep));
- }
-
- inline FW_CFixed operator/(FW_CFixed f1, FW_CFixed f2)
- {
- return FW_CFixed(ODFixedDivide(f1.fRep, f2.fRep));
- }
-
- #endif
-
- inline FW_Boolean operator>(FW_CFixed f1, FW_CFixed f2)
- {
- return f1.fRep > f2.fRep;
- }
-
- inline FW_Boolean operator==(FW_CFixed f1, FW_CFixed f2)
- {
- return f1.fRep == f2.fRep;
- }
-
- inline FW_Boolean operator<(FW_CFixed f1, FW_CFixed f2)
- {
- return f1.fRep < f2.fRep;
- }
-
- inline FW_Boolean operator>=(FW_CFixed f1, FW_CFixed f2)
- {
- return f1.fRep >= f2.fRep;
- }
-
- inline FW_Boolean operator!=(FW_CFixed f1, FW_CFixed f2)
- {
- return f1.fRep != f2.fRep;
- }
-
- inline FW_Boolean operator<=(FW_CFixed f1, FW_CFixed f2)
- {
- return f1.fRep <= f2.fRep;
- }
-
- //========================================================================================
- // CLASS FW_CWide
- //========================================================================================
-
- class FW_CLASS_ATTR FW_CWide
- {
- public:
- // ----- Construction
- friend FW_CWide FW_FUNC_ATTR FW_IntToWide(int i);
- friend FW_CWide FW_FUNC_ATTR FW_ODFixedToWide(ODFixed f);
-
- FW_CWide(FW_CFixed f);
-
- // ----- Conversion
- int AsInt() const;
- ODFixed AsODFixed() const;
-
- operator FW_CFixed() const;
-
- // ----- Arithmetic operators -----
-
- FW_CWide& operator += (const FW_CWide& w);
- FW_CWide& operator -= (const FW_CWide& w);
-
- friend FW_CWide FW_FUNC_ATTR operator + (const FW_CWide& w1, const FW_CWide& w2);
- friend FW_CWide FW_FUNC_ATTR operator - (const FW_CWide& w1, const FW_CWide& w2);
- friend FW_CWide FW_FUNC_ATTR FW_WideMultiply (FW_CFixed f1, FW_CFixed f2);
- friend FW_CFixed FW_FUNC_ATTR operator / (const FW_CWide& w1, FW_CFixed f2);
-
- // ----- Equality operators -----
-
- friend inline FW_Boolean operator == (const FW_CWide& w1, const FW_CWide& w2);
- friend inline FW_Boolean operator != (const FW_CWide& w1, const FW_CWide& w2);
- friend inline FW_Boolean operator > (const FW_CWide& w1, const FW_CWide& w2);
- friend inline FW_Boolean operator < (const FW_CWide& w1, const FW_CWide& w2);
- friend inline FW_Boolean operator >= (const FW_CWide& w1, const FW_CWide& w2);
- friend inline FW_Boolean operator <= (const FW_CWide& w1, const FW_CWide& w2);
-
- // ----- Transcendental methods
-
- FW_CFixed Sqrt() const;
-
- // ------ Implementation
- private:
- ODWide fRep;
-
- FW_CWide(const ODWide& w);
- FW_CWide(long hi, unsigned long lo);
- };
-
- //----------------------------------------------------------------------------------------
- // FW_CWide::FW_CWide
- //----------------------------------------------------------------------------------------
-
- inline FW_CWide::FW_CWide(const ODWide& w) :
- fRep(w)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWide::FW_CWide
- //----------------------------------------------------------------------------------------
-
- inline FW_CWide::FW_CWide(long hi, unsigned long lo)
- {
- fRep.hi = hi, fRep.lo = lo;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWide::FW_CWide
- //----------------------------------------------------------------------------------------
-
- inline FW_CWide& FW_CWide::operator += (const FW_CWide& w)
- {
- ODWideAdd(&fRep, &w.fRep);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWide::FW_CWide
- //----------------------------------------------------------------------------------------
-
- inline FW_CWide& FW_CWide::operator -= (const FW_CWide& w)
- {
- ODWideSubtract(&fRep, &w.fRep);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // operator ==
- //----------------------------------------------------------------------------------------
-
- inline FW_Boolean operator == (const FW_CWide& w1, const FW_CWide& w2)
- {
- return ODWideCompare(&w1.fRep, &w2.fRep) == 0;
- }
-
- //----------------------------------------------------------------------------------------
- // operator ==
- //----------------------------------------------------------------------------------------
-
- inline FW_Boolean operator != (const FW_CWide& w1, const FW_CWide& w2)
- {
- return ODWideCompare(&w1.fRep, &w2.fRep) != 0;
- }
-
- //----------------------------------------------------------------------------------------
- // operator ==
- //----------------------------------------------------------------------------------------
-
- inline FW_Boolean operator > (const FW_CWide& w1, const FW_CWide& w2)
- {
- return ODWideCompare(&w1.fRep, &w2.fRep) > 0;
- }
-
- //----------------------------------------------------------------------------------------
- // operator ==
- //----------------------------------------------------------------------------------------
-
- inline FW_Boolean operator < (const FW_CWide& w1, const FW_CWide& w2)
- {
- return ODWideCompare(&w1.fRep, &w2.fRep) < 0;
- }
-
- //----------------------------------------------------------------------------------------
- // operator ==
- //----------------------------------------------------------------------------------------
-
- inline FW_Boolean operator >= (const FW_CWide& w1, const FW_CWide& w2)
- {
- return ODWideCompare(&w1.fRep, &w2.fRep) >= 0;
- }
-
- //----------------------------------------------------------------------------------------
- // operator ==
- //----------------------------------------------------------------------------------------
-
- inline FW_Boolean operator <= (const FW_CWide& w1, const FW_CWide& w2)
- {
- return ODWideCompare(&w1.fRep, &w2.fRep) <= 0;
- }
-
- //========================================================================================
- // Global utilities
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CFixed
- //----------------------------------------------------------------------------------------
-
- inline FW_CFixed FW_Minimum(FW_CFixed f1, FW_CFixed f2)
- {
- return f1 < f2 ? f1 : f2;
- }
-
- inline FW_CFixed FW_Maximum(FW_CFixed f1, FW_CFixed f2)
- {
- return f1 > f2 ? f1 : f2;
- }
-
- inline FW_CFixed FW_Abosolute(FW_CFixed f)
- {
- return f > FW_IntToFixed(0) ? f : -f;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWide
- //----------------------------------------------------------------------------------------
-
- inline FW_CWide FW_Minimum(const FW_CWide& w1, const FW_CWide& w2)
- {
- return w1 < w2 ? w1 : w2;
- }
-
- inline FW_CWide FW_Maximum(const FW_CWide& w1, const FW_CWide& w2)
- {
- return w1 > w2 ? w1 : w2;
- }
-
- //========================================================================================
- // Global constants
- //========================================================================================
-
- extern const FW_CFixed FW_kFixed0; // 0
- extern const FW_CFixed FW_kFixedPos1; // +1
- extern const FW_CFixed FW_kFixedNeg1; // -1
- extern const FW_CFixed FW_kFixed72; // 72
- extern const FW_CFixed FW_kFixedPI; // pi
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export off
- #endif
-
- #endif
-